﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Serialization;

namespace WSBackup
{

    public class AppSettingsXml<T> where T : new()
    {
        private const string DEFAULT_FILENAME = "settings.xml";

        public void Save(string fileName = DEFAULT_FILENAME)
        {
            using (FileStream stream = new FileStream(fileName, FileMode.Create))
            {
                new XmlSerializer(typeof(T)).Serialize(stream, this);
            }
        }

        public static T Load(string fileName = DEFAULT_FILENAME)
        {
            T result = new T();

            if (File.Exists(fileName))
            {
                using (FileStream stream = File.OpenRead(fileName))
                {
                    result = (T)new XmlSerializer(typeof(T)).Deserialize(stream);
                }
            }

            return result;
        }
    }

}
